package leetcode_900;

/**
 *@author 周杨
 *SurfaceAreaOf3DShapes_892 输出一个三维柱体的表面积
 *describe:遍历即可 AC 28%
 *2018年11月12日 上午11:02:39
 */
public class SurfaceAreaOf3DShapes_892 {
	public int surfaceArea(int[][] grid) {
		int surfaceArea = 0;
        // 正视图
        for(int i = 0; i < grid.length; ++i)
        {
            for(int j = 0; j < grid[i].length; ++j)
            {
                //上下表面
                if(grid[i][j] > 0) surfaceArea += 2;
                // 左表面
                if(i - 1 >= 0 && grid[i-1][j] < grid[i][j]) 
                {
                    surfaceArea += (grid[i][j]-grid[i-1][j]);
                }
                else if(i - 1 < 0) surfaceArea += grid[i][j];
                // 右表面
                if(i + 1 < grid.length && grid[i+1][j] < grid[i][j]) 
                {
                    surfaceArea += (grid[i][j]-grid[i+1][j]);
                }
                else if(i + 1 >= grid.length) surfaceArea += grid[i][j];
                // 前表面
                if(j - 1 >= 0 && grid[i][j-1] < grid[i][j]) 
                {
                    surfaceArea += (grid[i][j]-grid[i][j-1]);
                }
                else if(j - 1 < 0) surfaceArea += grid[i][j];
                // 后表面
                if(j + 1 < grid.length && grid[i][j+1] < grid[i][j]) 
                {
                    surfaceArea += (grid[i][j]-grid[i][j+1]);
                }
                else if(j + 1 >= grid.length) surfaceArea += grid[i][j];
            }
        }
        return surfaceArea;
    }
}
